//
//  FTLibraries_FoundationValidation.m
//  FTLibraries_FoundationValidation
//
//  Created by JackieYip on 2017/6/10.
//  Copyright © 2017年 mondial-d. All rights reserved.
//

#import "FTLibraries_FoundationValidation.h"

id FTValidatedObject(id Object)
{
    if ([Object isKindOfClass:[NSNull class]])
        return nil;
    
    return Object;
}

NSString *FTValidatedString(NSString *string)
{
    if ([string isKindOfClass:[NSString class]])
        return string;
    else if ([string isKindOfClass:[NSNumber class]])
        return [(NSNumber *)string stringValue];
    else
        return @"";
}

NSNumber *FTValidatedNumber(NSNumber *number)
{
    if ([number isKindOfClass:[NSString class]]) {
        return (NSNumber *)number;
    }
    if ([number isKindOfClass:[NSNumber class]])
        return number;
    else
        return @(0);
}

NSArray *FTValidatedArray(NSArray *array)
{
    if ([array isKindOfClass:[NSArray class]])
        return array;
    return nil;
}

NSDictionary *FTValidatedDictionary(NSDictionary *dictionary)
{
    if ([dictionary isKindOfClass:[NSDictionary class]])
        return dictionary;
    return nil;
}
// ----------------------
NSArray <NSString *> *FTValidatedArrayContainString(NSArray <NSString *> *array)
{
    NSMutableArray *arrayM = [NSMutableArray array];
    array = FTValidatedArray(array);
    [array enumerateObjectsUsingBlock:^(NSString * _Nonnull string, NSUInteger idx, BOOL * _Nonnull stop) {
        string = FTValidatedString(string);
        if (string)
            [arrayM addObject:string];
    }];
    if (arrayM.count == 0) arrayM = nil;
    return arrayM;
}

NSArray <NSDictionary *> *FTValidatedArrayContainDictionary(NSArray <NSDictionary *> *array)
{
    NSMutableArray *arrayM = [NSMutableArray array];
    array = FTValidatedArray(array);
    [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull dictionary, NSUInteger idx, BOOL * _Nonnull stop) {
        dictionary = FTValidatedDictionary(dictionary);
        if (dictionary.count > 0) {
            [arrayM addObject:dictionary];
        }
    }];
    if (arrayM.count == 0) arrayM = nil;
    return arrayM;
}

NSArray <NSArray *> *FTValidatedArrayContainArray(NSArray <NSArray *> *array)
{
    NSMutableArray *arrayM = [NSMutableArray array];
    array = FTValidatedArray(array);
    [array enumerateObjectsUsingBlock:^(NSArray * _Nonnull subArray, NSUInteger idx, BOOL * _Nonnull stop) {
        subArray = FTValidatedArray(subArray);
        if (subArray.count > 0) {
            [arrayM addObject:subArray];
        }
    }];
    if (arrayM.count == 0) arrayM = nil;
    return arrayM;
}

// ----------------------
extern NSArray <NSArray <NSString *> *> *FTValidatedArrayContainArrayContainString(NSArray <NSArray <NSString *> *> *array)
{
    NSMutableArray *arrayM = [NSMutableArray array];
    array = FTValidatedArray(array);
    [array enumerateObjectsUsingBlock:^(NSArray<NSString *> * _Nonnull arrayContainString, NSUInteger idx, BOOL * _Nonnull stop) {
        arrayContainString = FTValidatedArrayContainString(arrayContainString);
        if (arrayContainString.count > 0) {
            [arrayM addObject:arrayContainString];
        }
    }];
    if (arrayM.count == 0) arrayM = nil;
    return arrayM;
}

extern NSArray <NSArray <NSDictionary *> *> *FTValidatedArrayContainArrayContainDictionary(NSArray <NSArray <NSDictionary *> *> *array)
{
    NSMutableArray *arrayM = [NSMutableArray array];
    array = FTValidatedArray(array);
    [array enumerateObjectsUsingBlock:^(NSArray<NSDictionary *> * _Nonnull arrayContainDictionary, NSUInteger idx, BOOL * _Nonnull stop) {
        arrayContainDictionary = FTValidatedArrayContainDictionary(arrayContainDictionary);
        if (arrayContainDictionary.count > 0) {
            [arrayM addObject:arrayContainDictionary];
        }
    }];
    if (arrayM.count == 0) arrayM = nil;
    return arrayM;
}
